# encoding=utf8
import pymysql,requests,urllib3,datetime,re
from lxml import etree
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

host = '127.0.0.1'
users = 'root'
pwd = '123123'
port = 3306
dbs = 'ans'


def mysql(系列,id,车型,类型,价格,是否上市):
    value = ((系列,id,车型,类型,价格,是否上市))
    db = pymysql.connect(host=host, user=users,passwd=pwd,port=port, db=dbs)
    cursor = db.cursor()
    sql = "INSERT INTO byd(系列,id,车型,类型,价格,是否上市) values(%s,%s,%s,%s,%s,%s)"
    try:
        cursor.execute(sql, value)
        db.commit()
        print('success!')
    except Exception as e:
        db.rollback()
        print("error.", e)
    db.close()


def downtext(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36 Edg/103.0.1264.37',
        # 'Cookie':'__ac_nonce=062b88429003a98705c41; msToken=kpPasTtpL66AVDYS4SGtLOx45tLUZkmX3HzeeZh8GhG_G18tEQuHVBT4zPyPT6Tf7Kx4_XlnRxPZkCq7sMAbF2iv5wrBrxt7Lr9Nou2ELCc9; __ac_signature=_02B4Z6wo00f015-KraAAAIDDH4hX4H095tefqqkAAIVi34; tt_webid=7105677063995246087; ttcid=7efd93ec14ac4307b113da0a7b5d5ffc38; local_city_cache=%E9%83%91%E5%B7%9E; csrftoken=7c185c6beed9fe2b823f5f075daaa648; _tea_utm_cache_24=undefined; ttwid=1%7CwyP_do6sR3svCFEVpZiaSYMdkjucj6U8171zM2_idW8%7C1656259628%7Ce1991d22649479f8e44afb9a5a1365aa220a811df8e933ddad53f2067fa14bd8; MONITOR_WEB_ID=06e34afb-d2ef-4ad9-85c3-fe58d188ac2e; tt_scid=J9gMb7zMDXtaapd8bFcONDvLPCR.Sp2aYn9-5wgcvSvhX1nxcKfDBQO9rN2S4XeRb6f4; s_v_web_id=verify_l4vi7x2f_2Zzab6rA_1rUn_4CmU_8XSm_hdI2how0Ds4M',

    }
    res = requests.get(url=url,headers=headers).text
    # res = re.findall('{"ClusterId": (.*?), "T',res)
    # print(res)
    return res
def downjson(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36 Edg/103.0.1264.37',
        # 'Cookie':'__ac_nonce=062b88429003a98705c41; msToken=kpPasTtpL66AVDYS4SGtLOx45tLUZkmX3HzeeZh8GhG_G18tEQuHVBT4zPyPT6Tf7Kx4_XlnRxPZkCq7sMAbF2iv5wrBrxt7Lr9Nou2ELCc9; __ac_signature=_02B4Z6wo00f015-KraAAAIDDH4hX4H095tefqqkAAIVi34; tt_webid=7105677063995246087; ttcid=7efd93ec14ac4307b113da0a7b5d5ffc38; local_city_cache=%E9%83%91%E5%B7%9E; csrftoken=7c185c6beed9fe2b823f5f075daaa648; _tea_utm_cache_24=undefined; ttwid=1%7CwyP_do6sR3svCFEVpZiaSYMdkjucj6U8171zM2_idW8%7C1656259628%7Ce1991d22649479f8e44afb9a5a1365aa220a811df8e933ddad53f2067fa14bd8; MONITOR_WEB_ID=06e34afb-d2ef-4ad9-85c3-fe58d188ac2e; tt_scid=J9gMb7zMDXtaapd8bFcONDvLPCR.Sp2aYn9-5wgcvSvhX1nxcKfDBQO9rN2S4XeRb6f4; s_v_web_id=verify_l4vi7x2f_2Zzab6rA_1rUn_4CmU_8XSm_hdI2how0Ds4M',

    }
    res = requests.get(url=url,headers=headers).json()
    # print(res)
    # print(type(res))
    return res
def func1(res,*系列):
    item_list = res['items']
    for item in item_list:
        系列 = str(系列)
        车型 = item['title']
        类型 = item['powerType']
        价格 = item['tiPrice']
        id = item['id']
        是否上市 = item['wetherListed']
        if 车型 == '' or 类型 == '' or 价格 == '' or id == '' or 是否上市 == '':
            continue
        print(系列,id,车型,类型,价格,是否上市)
        mysql(系列,id,车型,类型,价格,是否上市)

    return 0
def func(res):

    tree = etree.HTML(res)
    div_list = tree.xpath('//div[@id="main"]/div[2]/div[2]/div[1]/div[1]/div')
    for div in div_list:
        系列 = ''.join(div.xpath('./div/a/input/@value'))
        # print(系列)
        url = 'https://www.bydauto.com.cn/sites/REST/resources/v1/search/sites/BYD_AUTO/types/BydModelView/assets?fields=name,id,createdby,updatedby,title,wetherListed,isBooking,powerType,tiAfter,tiPrice,isBooking&field:subtype:equals=MotorDetails&field:carSeriesName:equals={}&orderBy=carSort:desc'.format(系列)
        func1(downjson(url),系列)




    return 0
if __name__ == "__main__":
    # for page in range(0,900,20):
    #     print('page',page)
        url = 'https://www.bydauto.com.cn/auto/index.html'
        # print(url)
        res = downtext(url)
        # print(res)
        func(res)
        # break

